import { message } from 'antd';

export default {
  // 获取格式化时间 get_format_date('yyyy-MM-dd')
  get_format_date: (fmt) => {
    fmt = fmt || 'yyyy-MM-dd hh:mm';
    var date = new Date();
    var o = {
      'M+': date.getMonth() + 1, //月份
      'd+': date.getDate(), //日
      'h+': date.getHours(), //小时
      'm+': date.getMinutes(), //分
      's+': date.getSeconds(), //秒
      'q+': Math.floor((date.getMonth() + 3) / 3), //季度
      S: date.getMilliseconds(), //毫秒
    };
    if (/(y+)/.test(fmt))
      fmt = fmt.replace(
        RegExp.$1,
        (date.getFullYear() + '').substr(4 - RegExp.$1.length),
      );
    for (var k in o)
      if (new RegExp('(' + k + ')').test(fmt))
        fmt = fmt.replace(
          RegExp.$1,
          RegExp.$1.length == 1
            ? o[k]
            : ('00' + o[k]).substr(('' + o[k]).length),
        );
    return fmt;
  },
  msg: (type, msg, time) => {
    time = time || 1;
    var msgs;
    switch (type) {
      case 'ok':
        msgs = message.success(msg, time);
        break;
      case 'war':
        msgs = message.warning(msg, time);
        break;
      case 'err':
        msgs = message.error(msg, 0);
        break;
      default:
        msgs = message.info(msg, time);
        break;
    }
    return msgs;
  },
  setClassName: (target, str, value) => {
    if (target.className.search(str) !== -1) {
      target.className = value;
    } else {
      target.parentNode.className = value;
    }
  },
  // 公共拖动函数
  drop: (e, m, tb, dp) => {
    // 判断是哪个元素接受拖入
    let self = e.dataTransfer.getData('Text');
    let op = document.getElementById(self).parentNode;
    let target;

    if (e.target.className.search('table') !== -1) {
      if (e.target.id) {
        target = e.target;
      } else {
        target = e.target.parentNode;
      }
    } else {
      if (e.target.id.search('tag') !== -1) {
        target = e.target;
      } else {
        target = e.target.parentNode;
      }
    }

    if (target.children.length < m) {
      if (target !== op) {
        console.log('拖入目标：', target, '拖出来源：', op, '自身：', self);

        var tmp_file, tmp_table1, tmp_table2;

        if (target.className.search('file_tags') !== -1) {
          console.log('需要添加file_tags');

          tmp_file = [...tb.imp_file_tags];
          tmp_file.push(self.substr(0, self.length - 1));
        } else {
          console.log('需要添加table_drop_box', target.id);
          tmp_table1 = [...tb.table_bd_tags[target.id]];
          tmp_table1.push(self.substr(0, self.length - 1));
        }

        if (op.className.search('file_tags') !== -1) {
          console.log('需要减少file_tags');

          tmp_file = [...tb.imp_file_tags];
          tmp_file = tmp_file.filter((i) => i !== self.substr(0, self.length - 1));
          tmp_file = tmp_file.filter((i) => i);
        } else {
          console.log('需要减少table_drop_box', op.id);

          tmp_table2 = [...tb.table_bd_tags[op.id]];
          tmp_table2 = tmp_table2.filter((i) => i !== self.substr(0, self.length - 1));
        }

        var data = Object.assign({}, tb.table_bd_tags);
        if (tmp_table1) data[target.id] = tmp_table1;
        if (tmp_table2) data[op.id] = tmp_table2;

        dp({
          type: 'table/save',
          payload: {
            table_bd_tags: data,
            imp_file_tags: tmp_file ? tmp_file : tb.imp_file_tags,
          },
        });
      }
    }
  },
  auto_to_select: (val) => {
    var re = {};
    val.forEach((e) => {
      re[e.value] = e.value;
    });
    return re;
  },
  get_select_op: (val) => {
    var re = {};
    val.forEach((e) => {
      re[e] = e;
    });
    return re;
  },
  get_radio_op: (val) => {
    var re = val.map((v) => ({ label: v, value: v }));
    return re;
  },
  waitTime: (time) => {
    time = time || 500;
    return new Promise((resolve) => {
      setTimeout(() => {
        resolve(true);
      }, time);
    });
  },
  set_query: (query) => {
    let keys = Object.keys(query);
    let q = {};
    keys.forEach((key) => {
      if(query[key]){
        if(query[key]===!!query[key]){
          q[key] = {$type:2};
        }else{
          q[key] = { $regex: query[key] };
        }
        
      }
    });
    return q;
  },
  print: () => {
    var printbox = document.createElement('div')
    var printbody = document.getElementById('print').cloneNode(true)
    var root = document.getElementById('root')
    printbox.setAttribute('id', 'printbox')
    printbox.appendChild(printbody)
    document.body.appendChild(printbox)
    root.style.display = 'none'
    window.print();
    document.body.removeChild(document.getElementById('printbox'))
    root.style.display = ''
  },
};
